package com.quanliang.org.crawlerquanliang.utils;

import com.quanliang.org.crawlerquanliang.pojo.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式工具类
 *
 * @author FYM
 * @data 2022/8/26 2:57
 */
public class RegularUtil {
    public static void main(String[] args) {
        String s = "div class=\"_3gcd_TVhABEQqCcXHsrIpT\"><a style=\"display:inline-block\" href=\"/blog/24579\" target=\"_self\"><div class=\"_1wTUfLBA77F7m-CM6YysS6 _3NQyvscVRjQNS9gEgQoDDo\" style=\"width:260px;height:166px\"><div class=\"_2ahG-zumH-g0nsl6xhsF0s\"><div style=\"display:block;overflow:hidden;position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;margin:0\"><noscript><img src=\"https://quan-1259287960.cos.ap-guangzhou.myqcloud.com/1000029/8899d9bc9c2f419b8e8ce72575c788f1\" decoding=\"async\" style=\"position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover\"/></noscript><img src=\"\" decoding=\"async\" style=\"position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover\"/></div>";
        System.out.println(getImgURL(s));
    }

    //从resp中提取多个Data大div集合
    public static final String DATAREG = "div class=\"_3gcd_TVhABEQqCcXHsrIpT\">([\\s\\S]+?)<\\/div>";

    //获取一堆div中间的数据
    public static final String GETDATAREG = ">.*</div>";
    //从bigDiv中获取标题
    //public static final String TITLEREG = "(<div class=\"_3_JaaUmGUCjKZIdiLhqtfr\">)([^<]*)(</div>)";
    public static final String TITLEREG = "div class=\"_3_JaaUmGUCjKZIdiLhqtfr\">([\\s\\S]+?)<\\/div>";
    //获取发布时间
    public static final String RELEASEDATE = "\\d{4}-\\d{2}-\\d{2}";
    //获取阅读量_step1
    public static final String READCOUNTREG_1 = "div class=\"_2gvAnxa4Xc7IT14d5w8MI1\">([\\s\\S]+?)<\\/div>";
    //获取阅读量_step2
    public static final String READCOUNTREG_2 = "\"\\d+\"";
    //获取URL中页码数
    public static final String PAGEREG = "[0-9]+";
    //获取图片URL_1
    //public static final String IMGURLREG_1 = "(<div class=\"_2ahG-zumH-g0nsl6xhsF0s\">)([^<]*)(</div>)";
    public static final String IMGURLREG_1 = "<div class=\"_2ahG-zumH-g0nsl6xhsF0s\">([\\s\\S]+?)<\\/div>";
    //获取图片URL_2
    public static final String IMGURLREG_2 = "src=.*decoding";
    //纯数字
    public static final String ONLYNUM = "^[0-9]+$";


    /**
     * 输入正则表达式，通用方法
     */
    private static List<String> getResult(String source, String reg) {
        List<String> strs = new ArrayList<>();
        Pattern p = Pattern.compile(reg, Pattern.DOTALL);
        Matcher m = p.matcher(source);
        while (m.find()) {
            strs.add(m.group());
        }
        return strs;
    }


    /**
     * 获取一对div中的数据
     */
    private static String getDivInnerData(String source) {
        Pattern p = Pattern.compile(GETDATAREG);
        Matcher m = p.matcher(source);
        if (m.find()) {
            String group = m.group();
            return group.substring(1, group.length() - 6);
        }
        return "";
    }


    /**
     * 通过resp正则返回Data集合对象
     * 已废弃
     */
    @Deprecated
    public static List<Data> getAllData(String resp) {
        ArrayList<Data> ans = new ArrayList<>();
        List<String> bigDataList = getResult(resp, DATAREG);
        for (String s : bigDataList) {
            //标题
            String title = getTitle(s);
            //发布日期
            String releaseDate = getReleaseDate(s);
            //阅读数
            String readCount = getReadCount(s);
            //图片地址
            String imgURL = getImgURL(s);

            Data data = new Data();
            data.setTitle(title);
            data.setReleaseDate(releaseDate);
            data.setReadCount(readCount);
            data.setImgURL(imgURL);
            ans.add(data);
        }
        return ans;
    }

    /**
     * 获取图片URL路径
     */
    private static String getImgURL(String s) {
        List<String> result = getResult(s, IMGURLREG_1);
        if (!result.isEmpty()) {
            List<String> result1 = getResult(result.get(0), IMGURLREG_2);
            return result1.isEmpty() ? "" : result.get(0);
        }
        return "";
    }

    /**
     * 获取阅读数
     */
    public static String getReadCount(String s) {
        Pattern p1 = Pattern.compile(READCOUNTREG_1);
        Matcher m1 = p1.matcher(s);
        if (m1.find()) {
            String ss = m1.group();
            Pattern p2 = Pattern.compile(READCOUNTREG_2);
            Matcher m2 = p2.matcher(ss);
            if (m2.find()) {
                return m2.group();
            }
        }
        return "";
    }

    /**
     * 获取发布时间
     */
    public static String getReleaseDate(String s) {
        List<String> result = getResult(s, RELEASEDATE);
        if (!result.isEmpty()) {
            return result.get(0);
        }
        return "";
    }

    /**
     * 获取标题
     */
    private static String getTitle(String s) {
        List<String> result = getResult(s, TITLEREG);
        if (!result.isEmpty()) {
            return getDivInnerData(result.get(0));
        }
        return "";
    }

    /**
     * 获取URL中的页码数
     * https://www.aquanliang.com/blog/page/2
     */
    public static String getPageByURL(String url) {
        List<String> result = getResult(url, PAGEREG);
        return result.isEmpty() ? "" : result.get(0);
    }

    /**
     * 判断是否为纯数字
     */
    public static boolean isOnlyNum(String s) {
        Pattern p = Pattern.compile(ONLYNUM);
        Matcher m = p.matcher(s);
        return m.matches();
    }
}
